﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace _3Dshower
{
    public enum direction { LEFT,RIGHT, DOWN, UP };
    public class line
    {
        private Point[] boundary;
        private direction RightDirection;
        private double C;//x=C,y=kx+b
        private double? k,b;
       
        public Point[] Boundary
        {
            get { return boundary; }
            set { boundary = value; }
        }
        public line(params Point[] pos)
        {
            Boundary = pos;
        }
        //根据所拥有的点数据进行重算显示面上的方程
        public void reCal()
        {
            if (boundary.Length != 2)
                ;//TODO: 抛出一个异常
            double[] p1Inf = boundary[0].Coor;//(x,y)
            double[] p2Inf = boundary[1].Coor;
            double dx = p1Inf[0] - p2Inf[0];
            double dy = p1Inf[1] - p2Inf[1];
            if ((dx>(-float.Epsilon))&&(dx<float.Epsilon))
            {
                k = null;
                b = null;
                C = p1Inf[0];
            }
            else
            {
                k = dy / dx;
                b = p1Inf[1] - k * p1Inf[0];
            }
        }
        //判断外界点与该直线的关系
        public direction judge(Point p)
        {
            double[] position = p.Coor;//(x,y)
            if (k.HasValue)
            {
                if ((k * position[0] + b - position[1]) > 0)//y-y0>0
                    return direction.DOWN;
                else
                    return direction.UP;
            }
            else
            {
                if (position[0] > C)
                    return direction.RIGHT;
                else
                    return direction.LEFT;
            }
        }

        //设置标准
        public void setStandard(Point po)
        {
            RightDirection = judge(po);
        }

        //以标准判断是否在合理的方向
        public bool judgeBaseStandard(Point po)
        {
            if (RightDirection != judge(po))
                return false;
            return true;
        }
    }
}
